<?php

class BonusSummary extends DataObject {
    private static $singular_name = "Bonus Summary";
    private static $plural_name = "Bonus Summaries";

    private static $db = array(
        'Date' => 'Date',
        'Status' => 'Varchar', // member status
        'CPSA' => 'Currency', // current personal sales amount
        'CADJPSA' => 'Currency', // current adjust personal sales amount
        'CPBV' => 'Int', // current personal bonus value
        'CADJPBV' => 'Int', // current adjust personal bonus value
        'CSDSA' => 'Currency', // current sponsor direct sales amount
        'CSDBV' => 'Int', // current sponsor direct bonus value
        'CSGBV' => 'Int', // current sponsor group bonus value
        'CLPGBV' => 'Int', // current left placement group bonus value
        'CADJLPGBV' => 'Int', // current adjust left placement group bonus value
        'CRPGBV' => 'Int', // current right placement group bonus value
        'CADJRPGBV' => 'Int', // current adjust right placement group bonus value
        'CFLPGBV' => 'Int', // carry forward left placement group bonus value
        'CFRPGBV' => 'Int', // carry forward right placement group bonus value
        'APSA' => 'Currency', // accumulate personal sales amount
        'AADJPSA' => 'Currency', // accumulate adjust personal sales amount
        'APBV' => 'Int', // accumulate personal bonus value
        'AADJPBV' => 'Int', // accumulate adjust personal bonus value
        'ASDSA' => 'Currency', // accumulate sponsor direct sales amount
        'ADSDSA' => 'Currency', // accumulate deduct sponsor direct sales amount
        'ASDBV' => 'Int', // accumulate sponsor direct bonus value
        'ASGBV' => 'Int', // accumulate sponsor group bonus value
        'ALPGBV' => 'Int', // accumulate left placement group bonus value
        'AADJLPGBV' => 'Int', // accumulate adjust left placement group bonus value
        'ARPGBV' => 'Int', // accumulate right placement group bonus value
        'AADJRPGBV' => 'Int', // accumulate adjust right placement group bonus value
        'SDDL' => 'Int', // sponsor direct downline
        'SDDL1K' => 'Int', // sponsor direct downline 1K
        'SDDL3K' => 'Int', // sponsor direct downline 3K
        'SGDL' => 'Int', // sponsor group downline
        'PGDL' => 'Int', // placement group downline
        'PLGDL' => 'Int', // placement left group downline
        'PRGDL' => 'Int', // placement right group downline
        'SPID' => 'Int', // sponsor parentid
        'SLEFT' => 'Int', // sponsor nleft
        'SRIGHT' => 'Int', // sponsor nright
        'PPID' => 'Int', // placement parentid
        'PLEFT' => 'Int', // placement nleft
        'PRIGHT' => 'Int', // placement nright
        'PPOS' => 'Int', // placement position
        'MBLV' => 'Int', // matching bonus level
        'BS1' => 'Currency', // bonus 1
        'BS2' => 'Currency', // bonus 2
        'BS3' => 'Currency', // bonus 3
        'BS4' => 'Currency', // bonus 4
        'BS5' => 'Currency', // bonus 5
        'NoUnilevelPayout' => 'Boolean',
		'NoPairingPayout' => 'Boolean',
		'NoMatchingPayout' => 'Boolean',
		'NoInvestPayout' => 'Boolean'
    );

    private static $default_sort = "Date DESC";

    private static $has_one = array(
        'Batch' => 'BonusBatch',
        'Member' => 'Member',
        'Group' => 'Group'
    );
	
	function onAfterWrite(){
		parent::onAfterWrite();
		
		if($this->Member()->exists()){
			$this->Member()->TotalPayout += ($this->BS1 + $this->BS2 + $this->BS3 + $this->BS4 + $this->BS5);
			$this->Member()->write();
		}
	}

    function BS1Link() {
        $view_link = sprintf('%s?%s=%s', BonusSummaryPage::get()->first()->Link('BS1'), 'BatchID', $this->BatchID);
        return sprintf('<a href="%s" title="%s" data-title="%s" rel="popup tooltip" data-modal-lg="true">%s</a>', $view_link, _t('BonusSummary.CLICK_PAYOUT_DETAILS', 'Click here to view payout details'), _t('BonusSummary.PAYOUT_DETAILS', 'Payout Details'), $this->dbObject('BS1')->Nice());
    }

    function BS2Link() {
        $view_link = sprintf('%s?%s=%s', BonusSummaryPage::get()->first()->Link('BS2'), 'BatchID', $this->BatchID);
        return sprintf('<a href="%s" title="%s" data-title="%s" rel="popup tooltip" data-modal-lg="true">%s</a>', $view_link, _t('BonusSummary.CLICK_PAYOUT_DETAILS', 'Click here to view payout details'), _t('BonusSummary.PAYOUT_DETAILS', 'Payout Details'), $this->dbObject('BS2')->Nice());
    }

    function BS3Link() {
        $view_link = sprintf('%s?%s=%s', BonusSummaryPage::get()->first()->Link('BS3'), 'BatchID', $this->BatchID);
        return sprintf('<a href="%s" title="%s" data-title="%s" rel="popup tooltip" data-modal-lg="true">%s</a>', $view_link, _t('BonusSummary.CLICK_PAYOUT_DETAILS', 'Click here to view payout details'), _t('BonusSummary.PAYOUT_DETAILS', 'Payout Details'), $this->dbObject('BS3')->Nice());
    }

    function BS4Link() {
        $view_link = sprintf('%s?%s=%s', BonusSummaryPage::get()->first()->Link('BS4'), 'BatchID', $this->BatchID);
        return sprintf('<a href="%s" title="%s" data-title="%s" rel="popup tooltip" data-modal-lg="true">%s</a>', $view_link, _t('BonusSummary.CLICK_PAYOUT_DETAILS', 'Click here to view payout details'), _t('BonusSummary.PAYOUT_DETAILS', 'Payout Details'), $this->dbObject('BS4')->Nice());
    }

    function BS5Link() {
        $view_link = sprintf('%s?%s=%s', BonusSummaryPage::get()->first()->Link('BS5'), 'BatchID', $this->BatchID);
        return sprintf('<a href="%s" title="%s" data-title="%s" rel="popup tooltip" data-modal-lg="true">%s</a>', $view_link, _t('BonusSummary.CLICK_PAYOUT_DETAILS', 'Click here to view payout details'), _t('BonusSummary.PAYOUT_DETAILS', 'Payout Details'), $this->dbObject('BS5')->Nice());
    }

    function canView($member = false) {
        return true;
    }

    function canEdit($member = false) {
        return false;
    }

    function canDelete($member = false) {
        return false;
    }

    function canCreate($member = false) {
        return false;
    }
}
?>